# !/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================
@Time : 2022/10/30 0030 8:43
@Author : 测试开发工程师
@File : black_handle.py
@Software: PyCharm
============================
"""

import os
import logging

# 注意：这里的 fun 就是代表 find，要把里面的 find 都改成 fun
import allure


def balck_wrapper(fun):
    def run(*args, **kwargs):
        from apptest.XueQiu_PO.base.base_page import BasePage
        basepage: BasePage = args[0]
        try:
            logging.info(f"开始查找元素:{args[2]}")
            return fun(*args, **kwargs)
        except Exception as e:
            logging.warning("未找到元素，处理异常")
            # 一旦发生错误，就截图
            curtime = basepage.get_time()
            tmp_name = curtime + ".png"
            tmp_path = os.path.join(os.path.dirname(__file__), "..", "images", tmp_name)
            basepage.screenshot(tmp_path)
            allure.attach.file(tmp_path, name="截图", attachment_type=allure.attachment_type.PNG)
            for balck in basepage.balck_list:
                logging.info(f"处理黑名单:{balck}")
                eles = basepage.driver.find_element(*balck)
                if len(eles) > 0:
                    logging.info(f"点击黑名单弹框")
                    eles[0].click()
                    return fun(*args, **kwargs)
                raise e
        return run
